package com.ztest.betsimu;

import com.apop.MainApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Random;

/**
 * @Description:
 * @Author: zjy
 * @Date: 2025/2/18 14:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class BetSimulation2 {

    @Test
    public void bettingTest() {
        // 当前资金100w
        int currentMoney = 500_0000;
        int stopBettingThreshold = currentMoney / 10;
        // 尝试次数
        int tryNum = 1000;
        // 初始球员价值5000
        int minMoney = 10000;
        int useMoney = minMoney;
        boolean lastSuccess = false;
        int continueSuccOrFailTime = 0;
        for (int i = 0; i < tryNum; i++) {

            currentMoney -= useMoney;
            if (currentMoney <= 0) {
                System.out.println("合同费不足，停止赌卡 ");
                break;
            }
            if (currentMoney <= stopBettingThreshold) {
                System.out.println("当前资金已经低于止损线，停止赌卡");
                break;
            }
            int lastUse = useMoney;

            boolean isSuccess = random1(48);
            if (lastSuccess == isSuccess && i > 0) {
                continueSuccOrFailTime++;
            } else {
                continueSuccOrFailTime = 0;
            }
            lastSuccess = isSuccess;

            if (!isSuccess) {
                useMoney = useMoney * 2;
            } else {
                currentMoney += useMoney * 2;
                useMoney = minMoney;
            }
            System.out.println("第" + (i + 1) + "次尝试，剩余资金： " + currentMoney
                    + (isSuccess ? "[突破成功]" : "[突破失败]") + (continueSuccOrFailTime > 0 ? continueSuccOrFailTime + 1 + ((isSuccess ? "连成" : "连黑")) : "") + " 当前投资 " + lastUse + " 下次投资 " + useMoney);

        }

    }

    /**
     * 模拟成功
     *
     * @param successRate 成功概率
     * @return
     */
    private boolean random1(int successRate) {
        // 生成1到100的随机整数
        int randomNumber = (int) (Math.random() * 100) + 1;
        System.out.println("随机数： " + randomNumber);
        if (randomNumber <= successRate) {
            return true;
        }
        return false;
    }


}
